package com.bluedon.ism.commons.util;

import org.springframework.util.ResourceUtils;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;

public class DutyUtil {
	private final static String DUTY_PROPERTIES = "classpath:duty_code.properties";
	private static Map<String,String> butyMap = null;
	
	
	/**
	 * 创建一个Map数据集合类型的getDutyMap方法
	 * 从duty_code.properties获取keySet
	 * 循环查询value数据
	 * @isRefresh 是否强制刷新
	 * @return hashMap
	 */
	public static  Map<String, String> getDutyMap(boolean isRefresh)  {
		if(butyMap == null || isRefresh){
			butyMap = new HashMap<String, String>();
			try {
				System.out.println(".....");
				Properties prop = new Properties();
				InputStream is = null;
				try {
					is = DutyUtil.class.getClassLoader().getResourceAsStream("duty_code.properties");
					if(is == null)
						is = new FileInputStream(ResourceUtils.getFile(DUTY_PROPERTIES));
				prop.load(is);
				}catch(IOException e) {
					e.printStackTrace();
				}
				Set<Object> keySet = prop.keySet();
				for(Iterator<Object> it = keySet.iterator();it.hasNext();){
					String key = (String)it.next();
					String value = prop.getProperty(key);
					butyMap.put(key, value);
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return butyMap;
	}
	/**
	 * 创建一个Map数据集合类型的getDutyMap方法
	 * 从duty_code.properties获取keySet
	 * 循环查询value数据
	 * @isRefresh 是否强制刷新 默认为false 
	 * @return hashMap
	 */
	public static  Map<String, String> getDutyMap()  {
		return getDutyMap(false);
	}
		
	public static void main(String[] args) throws Exception {
		System.out.println(getDutyMap());
		System.out.println(getDutyMap(true));
	}
} 

